home *** CD-ROM | disk | FTP | other *** search
- /***************************************
- * fix.h
- * fixed-point number class interface
- ***************************************/
-
- #pragma once
-
- #include <FixMath.h>
- #include <iostream.h>
- #include <assert.h>
-
- const Fixed fixed_zero = Long2Fix((long) 0);
-
- class fix {
- private:
- // arithmetic operators
- friend inline fix operator+(const fix, const fix);
- friend inline fix operator-(const fix, const fix);
- friend inline fix operator*(const fix, const fix);
- friend inline fix operator/(const fix, const fix);
- // I/O operators
- friend ostream& operator<<(ostream&, const fix&);
- friend istream& operator>>(istream&, fix&);
- // transcendental functions that are optimized for fixed-point numbers
- friend fix sin(const fix);
- friend fix cos(const fix);
- // data value
- Fixed n;
- // static data member for computation results
- static fix result;
- // private range check member function
- inline void rangecheck();
- public:
- // constructors
- fix() { n = fixed_zero; };
- fix(const long l) { n = Long2Fix(l); };
- fix(const long double d) { n = X2Fix(d); };
- // incremant and decrement operators
- fix operator++() { n += Long2Fix(1); return *this; }; // prefix
- fix operator--() { n -= Long2Fix(1); return *this; };
- fix operator++(int) { n += Long2Fix(1); return *this; }; // postfix
- fix operator--(int) { n -= Long2Fix(1); return *this; };
- // cast operators
- operator long double();
- // other operators...
- fix operator+=(const fix f) { *this = *this + f; return *this; };
- fix operator-=(const fix f) { *this = *this - f; return *this; };
- fix operator*=(const fix f) { *this = *this * f; return *this; };
- fix operator/=(const fix f) { *this = *this / f; return *this; };
- };
-
- inline void fix::rangecheck()
- {
- assert((n != 0x7FFFFFFF) && (n != 0x80000000));
- }
-
- inline fix operator+(const fix a, const fix b)
- {
- fix::result.n = a.n + b.n;
- return fix::result;
- }
-
- inline fix operator-(const fix a, const fix b)
- {
- fix::result.n = a.n - b.n;
- return fix::result;
- }
-
- inline fix operator*(const fix a, const fix b)
- {
- fix::result.n = FixMul(a.n,b.n);
- fix::result.rangecheck();
- return fix::result;
- }
-
- inline fix operator/(const fix a, const fix b)
- {
- fix::result.n = FixDiv(a.n,b.n);
- fix::result.rangecheck();
- return fix::result;
- }
-